$("#formSave").keyup(function(event){
	keyCode = event.which;
});
//精确查找商品
//$(document).off('click', '#jingqueczspyp');
//$(document).on('click','#jingqueczspyp',function(){
//	var mingcchen = $(this).parent().parent().find(".yaopinmc").val();
//	$(this).parent().parent().find(".yaopinmc").val('').addClass('qiaojie xinjianmb');
//	$(this).parent().parent().parent().find(".g-wmfinp-warning").css('display','none');
//	$(this).parent().parent().parent().find(".g-wmfinp-danger").css('display','none');
//	$(this).parent().parent().parent().find(".cgmdc-input3").val('');
//	$(this).parent().parent().parent().find(".cgmdc-input4").val('');
//	$(this).parent().parent().parent().find(".cgmdc-input51").val('');
//	$(this).parent().parent().parent().find(".cgmdc-input52").val('');
//	$(this).parent().parent().parent().find(".cgmdc-input61").val('');
//	$(this).parent().parent().parent().find(".cgmdc-input62").val('').attr('readonly',false);
//	$('.check').removeClass("check2").addClass("check1");
//	$('#checkHaocai').parent().parent('.modal-seh-leixing').css('display','block');
//	$('.modal-seh-laiyuan').css('display','none');
//	$('.chazhc').css('display','none');
//	$('.chazcy').css('display','none');
//	$(this).parent('ul').html('').css('display','none');
//	$("#shangpinjqcz").modal();
//	$('#shangpinmc').val(mingcchen);
//	$('#shengchancj').val('');
//	$('#gongyings').val('');
//	$('#checkHaocai').removeClass("check2").addClass("check1");
//	$('#checkBenzs').removeClass("check1").addClass("check2");
//	showJingq();
//});
$(function () {
	//form验证
	$('#formSave').validator({
		stopOnError:true,
		fields: {
			'dancijldw': '剂量单位:length(1~2)',
		}
	});
	/**正则判断字符数*/
	function getByteLen(val) {
		var len = 0;
		for (var i = 0; i < val.length; i++) {
			var a = val.charAt(i);
			if (a.match(/[^\x00-\xff]/ig) != null) {
				len += 2;
			} else {
				len += 1;
			}
		}
		return len;
	}
	//数量单位可输入
	$(document).on("blur","[name='dancijldw']",function(){
		var danwei = $(this).val();
		if(getByteLen(danwei) > 4) {
				notifyWarning('请输入1-2个字符');
				this.focus();
				$(this).addClass("n-invalid");
				return false;
			}else{
				$(this).removeClass("n-invalid");
		}
	});
	//数量单位可输入
	$(document).on("blur","[name='kaiyaodw']",function(){
		var kaiyaodw = $(this).val();
		if(getByteLen(kaiyaodw) > 4) {
				notifyWarning('请输入1-2个字符');
				this.focus();
				$(this).addClass("n-invalid");
				return false;
			}else{
				$(this).removeClass("n-invalid");
		}
	});
	// li标签传值
	$(document).on("mouseover",".dropdown-menu li",function(){
		$(this).addClass("hello");
	});
	$(document).on("mouseout",".dropdown-menu li",function(){
		$(this).removeClass("hello");
	});
//input模拟select模拟
 $(document).on('focusin',".dropdown-toggle",function(){
    $(this).siblings(".dropdown-menu").css("display","block");
 });
 //btn模拟select模拟
$(document).on("focusout",".dropdown-toggle",function(){
	if($(".dropdown-menu li").hasClass("hello")){
		$(this).siblings(".dropdown-menu").css("display","block");
	}else{
		$(this).siblings(".dropdown-menu").css("display","none");
	}
});

 $(document).on("click",".kaiyaodw li",function(){
    var bText = $(this).text();
    $(this).parent().siblings(".dropdown-toggle").val(bText).removeClass("n-invalid");;
    $(this).parent(".dropdown-menu").css("display","none");
    $(this).parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function(){
		if($(this).val()==""){
			$(this).focus();
			return false;
		}
	})
 });
 $(document).on("click",".cgmz-jcts li",function(){
	    var bText = $(this).text();
	    $(this).parent().siblings(".dropdown-toggle").val(bText).removeClass("n-invalid");;
	    $(this).parent(".dropdown-menu").css("display","none");
	    $(this).parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function(){
			if($(this).val()==""){
				$(this).focus();
				return false;
			}
		})
});
	$(document).on("mouseover",".cgmz-jcts li",function(){
	    var bText = $(this).text();
	    $(this).parent().siblings(".dropdown-toggle").val(bText).removeClass("n-invalid");;
	 });
 /*成药每次剂量为适量时，数量不可编辑*/
 $(document).on("click",".dancijldw li",function() { 
  	var bText = $(this).text();
  	 $(this).parent().siblings(".dropdown-toggle").val(bText).removeClass("n-invalid");;
      $(this).parent(".dropdown-menu").css("display","none");
 	var _text = $(this).parent().prev(".cgmdc-input52").val();
 	if(_text == "适量"){
 		$(this).parent().prev().prev(".cgmdc-input51").attr('disabled',true).val('').removeClass("n-invalid");;
 		 $(this).parent().parent().parent().parent().find("input:visible:not(.wm-group,.cgmdc-input51)").each(function(){
 			if($(this).val()==""){
 				$(this).focus();
 				return false;
 			}
 		})
 	}else{
 		$(this).parent().prev().prev(".cgmdc-input51").attr('disabled',false);
 		 $(this).parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function(){
 			if($(this).val()==""){
 				$(this).focus();
 				return false;
 			}
 		})
 	}
   
 });
//点击用药频率li
 $(document).on("click",".yongyyaoplul li",function(){
 	var bText = $(this).attr('index');
 	$(this).parent().parent().find('.yongyypl').val(bText).removeClass("n-invalid");;
 	$(".f-input-li").css("display","none");
 	$(this).parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function(){
		if($(this).val()==""){
			$(this).focus();
			return false;
		}
	});
 }); 

 //点击用法li
 $(document).on("click",".yongfaul li",function(){
 	var bText = $(this).text();
 	$(this).parent().parent().find('.yongfa').val(bText).removeClass("n-invalid");;
 	$(".f-input-li").css("display","none");
 	$('#hz_serch').val("");
 	$(this).parent().parent().parent().parent().find("input:visible:not('.wm-group')").each(function(){
		if($(this).val()==""){
			$(this).focus();
			return false;
		}
	});
 });
});
var dwid;
var dwPage;
var dwmingcheng;
var dwleibie;
function getId(id,predwPage,predwmingcheng,predwleibie, preyuanGongId) {
	dwPage=predwPage;
	dwmingcheng=predwmingcheng;
	dwleibie=predwleibie;
	dwid=id;
	$.ajax({
	    type:"get",
	    //获取json文件路径
	    url:"./chufangmb/getById",
	    //传送数据
	    data:{id:id},
	    //请求成功完成后要执行的方法
	    success: function(data){
	    	if (preyuanGongId == data.chufangmb.suoyouzid) {
	    		$('#btnSubmit').css('display', 'inline-block');
	    		$('#fenxiangdiv').css('display', 'block');
	    	}
	        $('#mbmc').attr('value',data.chufangmb.mingcheng);
	        if(data.chufangmb.jinyongbz==1) {
				$('#status-work').attr('checked','checked');
				$('#work-label').attr('class','radio-label on');
	        }else{
	        	$('#status-stop').attr('checked','checked');
	        	$('#stop-label').attr('class','radio-label on');
	        }
	        if(data.chufangmb.fenxiangbz==1) {
				$('#status-fenxiang').attr('checked','checked');
				$('#fenxiang-label').attr('class','radio-label on');
	        }else{
	        	$('#status-bufenxiang').attr('checked','checked');
	        	$('#bufenxiang-label').attr('class','radio-label on');
	        }
	        var chenyaocfyps = data.exChenyaocfyps;
	        	var str = "";
				$(chenyaocfyps).each(function(index,value){
					var zuhao = this.zuhao;
					var dancijl = this.dancijl;
					var kaiyaoliang = this.kaiyaoliang;
					if(zuhao==null||zuhao==0) {
						zuhao="";
					}
					if(dancijl==null) {
						dancijl="";
					}
					if(kaiyaoliang==null) {
						kaiyaoliang="";
					}
					var dancijldw = this.dancijldw;
					var zhensuoypid = this.zhensuoypid;
					if(zhensuoypid==null){
						str += '<tr><td class="dropdown-search-li"><input type="text" class="wm-medicine-name cgmdc-input1 dropdown-toggle yaopinmc" data-rule="药品名称:length(1~100)" data-target="#msg_holder" name="yaopinmc" value="'+this.yaopinmc+'" onblur="yaowuxk(this)" autocomplete="off"><input type="hidden" class="wm-paixu" style="display:none;" name="paixu" value="1"><div class="g-wmfinp-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content="该药品不在您的诊所库存中" style="display:block;"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><div class="g-wmfinp-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none" role="menu"></ul></td>';
					}else{
					str += '<tr><td class="dropdown-search-li"><input type="text" class="wm-medicine-name cgmdc-input1 dropdown-toggle yaopinmc" data-rule="药品名称:length(1~100)" data-target="#msg_holder" name="yaopinmc" value="'+this.yaopinmc+'" onblur="yaowuxk(this)" autocomplete="off"><input type="hidden" class="wm-paixu" style="display:none;" name="paixu" value="1"><div class="g-wmfinp-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><div class="g-wmfinp-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none" role="menu"></ul></td>';
					}
					str += '<td><input type="text" value="'+zuhao+'" class="wm-group cgmdc-input2 dropdown-toggle" data-rule="组号:integer[+];range[~99]" data-target="#msg_holder"  name="zuhao" autocomplete="off"><ul class="dropdown-menu cgmz-jcts"style="width: 52px;" role="menu"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul></td>'+
			        '<input type="hidden"  style="display:none;" name="zhensuoypid" value="'+this.zhensuoypid+'">'+
			        '<input type="hidden"  style="display:none;" name="pingtaiypid" value="'+this.pingtaiypid+'">'+
			        '<input type="hidden"  style="display:none;" name="ptuuid" value="'+this.ptuuid+'">'+
			         '<td class="dropdown-search-li"><input type="text" value="'+this.yongfa+'" class="input-sm  cgmdc-input3 dropdown-toggle yongfa" style="width:100%" name="yongfa" data-rule="用法:length(1~64)" data-target="#msg_holder" onfocus="getYongfazd(this)" onkeyup="getYongfazd(this)" autocomplete="off"><ul class="dropdown-menu li-width f-input-li yongfaul"   style="display:none" role="menu"></ul></td>'+
			         '<td class="dropdown-search-li"><input value="'+this.yongyaopl+'" class="wm-rate dropdown-toggle yongyypl cgmdc-input4" data-rule="用药频率:length(1~64)" data-target="#msg_holder" name="yongyaopl" onfocus="getZDYongyaoplBymc(this)" onkeyup="getZDYongyaoplBymc(this)" autocomplete="off"><ul class="dropdown-menu li-width f-input-li yongyyaoplul"  style="display:none" role="menu"></ul></td>'+
			         '<td class="wm-dosage-father dropdown-search-li">';
						if(dancijldw=='适量') {
							str +='<input type="text"  class="wm-dosage cgmdc-input51" name="dancijl" data-rule="单次剂量:range[0~];length(~8)" data-target="#msg_holder" autocomplete="off" disabled="disabled"> ' ;
						}else{
							str +='<input type="text" value="'+this.dancijl+'" class="wm-dosage cgmdc-input51" name="dancijl" data-rule="单次剂量:range[0~];length(~8)" autocomplete="off" data-target="#msg_holder"> ' ;
						}
						str +='<input value="'+this.dancijldw+'" class="wm-dosage dropdown-toggle cgmdc-input52" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" name="dancijldw" autocomplete="off">'+
			          	'<ul class="dropdown-menu li-width f-input-li dancijldw"   style="display:none" role="menu">'+
			          	'<li>片</li>'+
                 		'<li>粒</li>'+
                 		'<li>支</li>'+
                 		'<li>袋</li>'+
                 		'<li>丸</li>'+
                 		'<li>枚</li>'+
                 		'<li>瓶</li>'+
                 		'<li>滴</li>'+
                 		'<li>克</li>'+
                 		'<li>g</li>'+
                 		'<li>ml</li>'+
            	 		'<li class="wushuju" style="padding-left:0;text-align:center;">适量</li>'+
						   '</ul>'+	
			         '</td>'+
			         '<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
			          	'<input  value="'+this.kaiyaoliang+'" type="text" class="wm-number cgmdc-input61" name="kaiyaoliang" data-rule="开药量:integer[+];length(~8)" data-target="#msg_holder" autocomplete="off"> ';
			          	if(this.zhensuoypid != null){
			          		if(this.chailingbz == 0||this.chailingbz == null){
			          			str +='<input value="'+this.kaiyaodw+'" class="wm-dosage dropdown-toggle cgmdc-input62" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" readonly="readonly" name="kaiyaodw" style="background-color:#efefef;" autocomplete="off">'+
					          	'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none" role="menu">'+
								   '</ul></td>';
			          		}else{
			          			str +='<input value="'+this.kaiyaodw+'" class="wm-dosage dropdown-toggle cgmdc-input62" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" name="kaiyaodw" autocomplete="off" style="background:#fff;">'+
					          	'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none" role="menu"><li>'+this.chailingdw+'</li><li>'+this.baozhuangdw+'</li>'+
								   '</ul></td>';
			          		}
			          		
			          	}else{
			          		str +='<input value="'+this.kaiyaodw+'" class="wm-dosage dropdown-toggle cgmdc-input62" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" name="kaiyaodw" autocomplete="off" readOnly>'+
				          	'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none" role="menu">'+
							   '</ul></td>';
			          	}
			          	
						   str +='<input type="hidden" name="danjia" value="">'+
					 '<td><div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div></td>'+
			        '</tr>';
				});
				
					str += '<tr><td class="dropdown-search-li"><input type="text" class="wm-medicine-name cgmdc-input1 dropdown-toggle yaopinmc" data-rule="药品名称:length(1~100)" data-target="#msg_holder" name="yaopinmc" value="" onkeyup="getYaopin(this,2)" onblur="yaowuxk(this)" autocomplete="off"><input type="hidden" class="wm-paixu" style="display:none;" name="paixu" value="1"><div class="g-wmfinp-warning"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><div class="g-wmfinp-danger"  data-container="body" data-toggle="popover" data-placement="bottom"  data-content=""><i class="fa fa-exclamation-triangle" aria-hidden="true"></i></div><ul class="dropdown-menu li-width f-input-li yaopinmcul"   style="display:none" role="menu"></ul></td>'+
			        '<td><input type="text" value="" class="wm-group dropdown-toggle cgmdc-input2" data-rule="组号:integer[+];range[~99]" data-target="#msg_holder"  name="zuhao" autocomplete="off"><ul class="dropdown-menu cgmz-jcts"style="width: 52px;" role="menu"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul></td>'+
			        '<input type="hidden"  style="display:none;" name="zhensuoypid" value="">'+
			        '<input type="hidden"  style="display:none;" name="pingtaiypid" value="">'+
			        '<input type="hidden"  style="display:none;" name="ptuuid" value="">'+
			         '<td class="dropdown-search-li"><input type="text" value="" class="input-sm  cgmdc-input3 dropdown-toggle yongfa" style="width:100%" name="yongfa" data-rule="用法:length(1~64)" data-target="#msg_holder" autocomplete="off"><ul class="dropdown-menu li-width f-input-li yongfaul"   style="display:none" role="menu"></ul></td>'+
			         '<td class="dropdown-search-li"><input value="" class="wm-rate dropdown-toggle yongyypl cgmdc-input4" data-rule="用药频率:length(1~64)" data-target="#msg_holder" name="yongyaopl" onfocus="getZDYongyaoplBymc(this)" onkeyup="getZDYongyaoplBymc(this)" autocomplete="off"><ul class="dropdown-menu li-width f-input-li yongyyaoplul"  style="display:none" role="menu"></ul></td>'+
			         '<td class="wm-dosage-father dropdown-search-li">'+
					'<input type="text" value="" class="wm-dosage cgmdc-input51" name="dancijl" data-rule="单次剂量:range[0~];length(~8)" data-target="#msg_holder" autocomplete="off"> ' +
					'<input value="" class="wm-dosage dropdown-toggle cgmdc-input52" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" name="dancijldw" autocomplete="off">'+
			          	'<ul class="dropdown-menu li-width f-input-li dancijldw"   style="display:none" role="menu">'+
			          	'<li>片</li>'+
                 		'<li>粒</li>'+
                 		'<li>支</li>'+
                 		'<li>袋</li>'+
                 		'<li>丸</li>'+
                 		'<li>枚</li>'+
                 		'<li>瓶</li>'+
                 		'<li>滴</li>'+
                 		'<li>克</li>'+
                 		'<li>g</li>'+
                 		'<li>ml</li>'+
            	 		'<li class="wushuju" style="padding-left:0;text-align:center;">适量</li>'+
						   '</ul>'+	
			         '</td>'+
			         '<td class="wm-dosage-father wm-dosage-father2 dropdown-search-li">'+
			          	'<input  value="" type="text" class="wm-number cgmdc-input61" name="kaiyaoliang" data-rule="开药量:integer[+];length(~8)" data-target="#msg_holder" autocomplete="off"> ' + 
			          	'<input value="" class="wm-dosage dropdown-toggle cgmdc-input62" data-rule="剂量单位:length(1~10)" data-target="#msg_holder" name="kaiyaodw" autocomplete="off" readOnly>'+
			          	'<ul class="dropdown-menu li-width f-input-li kaiyaodw"  style="display:none" role="menu">'+
						   '</ul></td>'+
					'<input type="hidden" name="danjia" value="">'+
					 '<td><div class="g-mz-delete pointer2 shanchu" data-container="body" data-toggle="popover" data-placement="bottom"  data-content="清除本行"></div></td>'+
			        '</tr>';
				$('#first1').replaceWith(str);
	    }
	});
}
//成药药品添加一行
$(document).off("focusout",'.western-medicine tbody tr .wm-medicine-name');
$(document).on('focusout','.western-medicine tbody tr .wm-medicine-name',function(){
	var $parents = $(this).parent().parent().parent('tbody');
	var $parent = $parents.children("tr");
	var len=$parent.length;
    var isLast = $parents.find(".wm-medicine-name").last().is($(this));
    if(isLast&&$(this).val()!=""){
    	$(this).parent().parent('tr').after(shengchengchengyyhMoney(len)); 
    }
		
});

//点击药品名称li
$(document).on("click",".yaopinmcul li.xuanzeli",function(){
	xuanzeypFlag = true;
	var id = $(this).attr('index');
	var iszs = $(this).attr('iszs');
	var yaopin = getYaopinById(id,iszs);
	fillchufangmb(id,iszs,yaopin,$(this));
});
function huoQuGuangBiao(yuanshu,msg){
	notifyWarning(msg);
	yuanshu.focus();
};
//点击保存时
var xuanzeypFlagbf = true;
vilidateform();
function vilidateform() {
   	  $('#formSave').on('valid.form', function(e){
			$('#hichengyaocf').val(divDataToJson('western-medicine'));
			var guangBiao = true;;
			//成药处方
	 			$('.western-medicine').each(function(){
	 				$(this).find("tbody tr").each(function(){
 	 	  				var yz1=$(this).find('.cgmdc-input1').val();
 	 	  				var yz2=$(this).find('.cgmdc-input2').val();
 	 	  				var yz3=$(this).find('.cgmdc-input3').val();
 	 	  				var yz4=$(this).find('.cgmdc-input4').val();
	 	 	  			var yz51=$(this).find('.cgmdc-input51').val();
 	 	  				var yz52=$(this).find('.cgmdc-input52').val();
 	 	  				var yz61=$(this).find('.cgmdc-input61').val();
 	 	  				var yz62=$(this).find('.cgmdc-input62').val();
	 	  				if(yz1!=""||yz3!=""||yz4!=""||yz51!=""||yz52!=""||yz61!=""||yz62!=""){
	 	  					if(yz1==""){
	 	  						$(this).find('.cgmdc-input1').addClass('n-invalid');
	 	  					if(guangBiao)
	 	  					huoQuGuangBiao($(this).find('.cgmdc-input1'),'请完成成药名称');
	 	  					guangBiao = false;
	 	  					}
	 	  					if(yz3==""){
	 	  					$(this).find('.cgmdc-input3').addClass('n-invalid');
	 	  				if(guangBiao)
	 	  				huoQuGuangBiao($(this).find('.cgmdc-input3'),'请完成成药用法');
	 	  				guangBiao = false;
	 	  					}
	 	  					if(yz4==""){
	 	  					$(this).find('.cgmdc-input4').addClass('n-invalid');
	 	  				if(guangBiao)
	 	  				huoQuGuangBiao($(this).find('.cgmdc-input4'),'请完成成药用药频率');
	 	  				guangBiao = false;
	 	  					}
	  					if(yz51==""){
		 	 	  			if(guangBiao){
		 	 	  				if($(this).find('.cgmdc-input52').val()=="适量"){	
		 	 	  				}else{
		 	 	  				$(this).find('.cgmdc-input51').addClass('n-invalid');
		 	 	  					huoQuGuangBiao($(this).find('.cgmdc-input51'),'请完成成药每次剂量数');
		 	 	  					guangBiao = false;
		 	 	  				}
		 	 	  			}
	  					}
	 	  				if(yz52==""){
	 	  					$(this).find('.cgmdc-input52').addClass('n-invalid');
	 	  				if(guangBiao)
	 	  				huoQuGuangBiao($(this).find('.cgmdc-input52'),'请完成成药每次剂量单位');
	 	  				guangBiao = false;
	 	  					}
	 	  					if(yz61==""){
	 	  					$(this).find('.cgmdc-input61').addClass('n-invalid');
	 	  				if(guangBiao)
	 	  				huoQuGuangBiao($(this).find('.cgmdc-input61'),'请完成成药开药数量');
	 	  				guangBiao = false;
	 	  					}
	 	  				if(yz62==""){
	 	  				$(this).find('.cgmdc-input62').addClass('n-invalid');
	 	 	  			if(guangBiao)
	 	 	  			huoQuGuangBiao($(this).find('.cgmdc-input62'),'请进入库存模块，维护药品');
	 	 	  			guangBiao = false;
	 	  					}
	 	  					
	 	  				}
	 	  				if(yz1==""||yz3==""||yz4==""||yz51==""||yz52==""||yz61==""||yz62==""){
	  	  	  			  	return false;
	  					}
	 				});
	 			});
				if(!xuanzeypFlagbf){
					xuanzeypFlag = true;
				}
				//提交西药
				if(guangBiao&&xuanzeypFlag){
					var chengyaoyp = tableDataToJson("chengyaoyp");
					$('#chengyaoypId').attr('value',chengyaoyp);
					$('#id').attr('value',id);
					$.ajax({
			    		url:'./chufangmb/xiugaichengyaocfmb',
			    		type:'POST',
			    		 beforeSend: function () {
		 					$("#loading").css({"display":"block","z-index":"5"})
		 					 },
	 					 complete: function () {
	 						$("#loading").css({"display":"none","z-index":"5"})
	 					 },
			    		data:$('#formSave').serializeArray(),
			    		success:function(data) {
	 						if (data == MSG_CHONGFUBZ) {
	 							notifyWarning("该处方在诊所中已存在");
	 							return false;
	 						}
							var msg='成药处方保存成功';
							var sjc=new Date().getTime();
							msg=encodeURI(encodeURI(msg));
			    			$("#cedan").load(path+"/views/zhensuo/chufangmb.jsp?msg="+msg+"&sjc="+sjc+"&dwid="+dwid+"&dwPage="+dwPage+"&dwmingcheng="+dwmingcheng+"&dwleibie="+dwleibie + '&jinyongbz=' + jinyongbz);
			    			return false; 
			    		}
			    	});
					
				}
   	  });
}		
$(function(){
 $(".radio-label").on("click",function(){
	  $(this).addClass("on").siblings().removeClass("on");
	  if($("#west-label").hasClass("on")){
	  		$(".g-m-chinese").css("display","none");
	  		$(".g-m-west").css("display","block");
	  		}else{
	  		$(".g-m-chinese").css("display","block");
	  		$(".g-m-west").css("display","none");
	  }; 
	});
});
$(function(){
	  $(this).addClass("on").siblings().removeClass("on");
	  if($("#west-label").hasClass("on")){
	  		$(".g-m-chinese").css("display","none");
	  		$(".g-m-west").css("display","block");
	  		$("#chinese-label").css("display","none");
	  		}else{
	  		$(".g-m-chinese").css("display","block");
	  		$(".g-m-west").css("display","none");
	  		$("#west-label").css("display","none");
	  }; 
});
	$(function () {
	$(".add-western").click(function () {
		var kelong=$("#western-medicine").clone(true);
		$("#western-medicine").after(kelong);   //在表格后面添加一行
		
	});
	
	$(document).on("click",".pointer3",function(){
            var $this=$(this);
            var $parent=$this.parent();
            var $tr=$parent.parent().parent();
            var length=$(".chinese-medicine tbody tr").length;
		if (length<=1) {
		} else {
			$(this).parent().parent().remove();
            $parent.slideUp(300,function(){
                $tr.find(".zymb-number").each(function(i,e){
                    $(e).text(+(i+1));
                });
            });
            }
 	});
	$(".btn_addchinese").click(function () {
		var kelong=$(".chinese-medicine tbody tr:last").clone(true);
			$(this).parent().parent().siblings("tbody").append(kelong);  //在表格后面添加一行
	});

	$("pointer1").click(function(){
		var western=$(".western-medicine").length;
		var chinese=$(".chinese-medicine").length;
		if (western<= 1||chinese<=1) {
			console.log("至少保留一副药单");
		} else {
			$(this).parent().parent().remove();//移除当前行
		}
	});
	$(document).on('click','.go-up',function(){
		var a =$(this).parent().parent();
		var b =$(this).parent().parent().prev();
		$(a).insertBefore(b);
		var $this=$(this);
            var $parent=$this.parent();
            var $tr=$parent.parent().parent();
                $tr.find(".zymb-number").each(function(i,e){
                    $(e).text(+(i+1));
                });
		
	});
	$(document).on('click','.go-down',function(){
		var a =$(this).parent().parent();
		var b =$(this).parent().parent().next();
		$(b).insertBefore(a);
		var $this=$(this);
            var $parent=$this.parent();
            var $tr=$parent.parent().parent();
                $tr.find(".zymb-number").each(function(i,e){
                    $(e).text(+(i+1));
                });
	});
	$(document).on('click','.pointer2',function(){
		var $this=$(this);
		var length=$(".western-medicine tbody tr").length;
		
		var $parents = $(this).parent().parent().parent('tbody');
        var wmName =$this.parents("tr").find(".wm-medicine-name");
        var isLast = $parents.find(".wm-medicine-name").last().is(wmName);
        if ((length<=1)||isLast) {
			$this.parent().parent().find("input:text").val("").removeClass("n-invalid");
			$this.parent().parent().find(".g-wmfinp-warning").css("display","none");
			$this.parent().parent().find(".g-wmfinp-danger").css("display","none");
		} else {
			$(this).parent().parent().remove();
			xuanzeypFlagbf = false;
		}
		xuanzeypFlagbf = false;
	});
});
	
	$(document).on('input','.yaopinmc',function(){
		var parents = $(this).parents('.western-medicine');
		if($(parents).attr('class')!=undefined) {
			getYaopin(this,2, '1');
		}else{
			getYaopin(this,1, '1');
		}
		
	});
	$(document).on("focusout",".yaopinmc",function(){
		if($(this).val()==''){
			xuanzeypFlag = true;
		}
	});
	var jixing = '';
	//用法点击聚焦事件
	$(document).on('focusin','[name="yongfa"]',function(){
		getYongfazd(this,jixing);
	});
	//整体去掉input的autocomplete
	$("input:text").attr("autocomplete","off");
	//回车转Tab
	//检查控件是否可见
	function checkobjvisual(e) {


	    var objvis = false;
	    if (e) {
	        if (e.offsetHeight > 0) {
	            objvis = true;
	        }
	        if (e.offsetTop > 0) {
	            objvis = true;
	        }
	        
	         if (e.readOnly == true) {
	             objvis = false;  
	       }  
	      
	    }
	    return objvis;
	}



	//查找下一个控件
	function focunext(next_id) {


	    var code;
	    if (!e) {
	        var e = window.event;
	    }
	    if (e.keyCode) {
	        code = e.keyCode;
	    }
	    else if (e.which) {
	        code = e.which;
	    }
	    if (code == 13) {


	        if (next_id)
	        {document.getElementById(next_id).focus(); }
	        else {
	            var inputList = document.getElementsByTagName("input");
	            // 循坏这个集合，包括了所有的input。
	            var nextindex;
	            nextindex = -1;
	            for (i = 0; i < inputList.length; i++) {
	                if (inputList[i] == document.activeElement) {
	                    var acobj = document.activeElement;
	                    nextindex = i + 1;
	                    
	                    while ((!checkobjvisual(inputList[nextindex])) && (nextindex <= inputList.length)) {
	                        nextindex = nextindex + 1;
	                    }
	                    if (inputList[nextindex])  inputList[nextindex].focus();
	                    break;
	                }
	            }
	            
	        }
	    }
	};
	document.onkeydown = function(e) {
		    var code;
		    if (!e) {
		        var e = window.event;
		    }
		    if (e.keyCode) {
		        code = e.keyCode;
		    }
		    else if (e.which) {
		        code = e.which;
		    }
		    if (code == 8) {
		        if (document.activeElement.readOnly == true || document.activeElement.disabled == true)
		        { return false; }
		    }
		    if (code == 13) {
		        if (document.activeElement.type == "textarea") {
		            return;
		        }
		        if (document.activeElement.type != "submit" && document.activeElement.type != "button") {
		 
		             event.keyCode = 9;
		             if(!enterToTab){
				    		enterToTab = true;
				    }else{
				    	focunext();
				    }
		            return false;
		        }
		    }
		   
	}
